package com.fy.sys.controller;

import com.fy.core.vo.ApiRest;
import com.fy.sys.service.DeptService;
import com.fy.sys.vo.DepartTreeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.fy.core.dto.PageDto;
import com.fy.core.vo.ApiRest;
import com.fy.core.vo.PageResult;
import com.fy.repo.entity.Repo;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import com.fy.sys.service.DeptService;
import com.fy.sys.entity.Dept;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * <p>
 * 部门表 前端控制器
 * </p>
 *
 * @author lz
 * @since 2023-06-14
 */
@RestController
@RequestMapping("/dept")
@CrossOrigin
public class DeptController {


    @Autowired
    private DeptService deptService;



    @PostMapping("/pageFind")
    public PageResult<Dept> findPage(@RequestBody PageDto<Dept> params){
        return deptService.findPage(params);
    }
    @RequestMapping("/findDepts")
    public ApiRest<List<Dept>> findDepts(){
        return ApiRest.success(deptService.findDepts());
    }

    @PostMapping("/addDept")
    public ApiRest addDept(@RequestBody Dept dept){
         deptService.add(dept);
        return ApiRest.success("添加成功","");
    }

    @PostMapping(value = "/delete")
    public ApiRest delete( Long deptId) {

        deptService.updateByDelflag(deptId);
        return ApiRest.success("deleted successfully");
    }

    @PostMapping(value = "/delete/batch")
    public ApiRest<Object> delete(@RequestBody List<Dept> deptList) {
        return deptService.batchDelete(deptList);
    }

    @PostMapping(value = "/update")
    public ApiRest<Object> delete(@RequestBody Dept dept) {

                deptService.updateById(dept);

        return ApiRest.success("deleted successfully","");
    }


    @PostMapping(value = "/updateStatus")
    public ApiRest<Object> updateStatus(Long deptId) {

        deptService.updateByStatus(deptId);

        return ApiRest.success("状态修改成功","");
    }


    @GetMapping("tree")
    public ApiRest<List<DepartTreeVo>> tree(){
        return deptService.findDepartCascade();
    }
}
